package com.huichenghe.xinlvsh01;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import com.huichenghe.xinlvsh01.Ble.BleForPhoneAndSmsRemind;
import com.huichenghe.xinlvsh01.Utils.BreakPhoneUtils;
import com.huichenghe.xinlvsh01.Utils.LocalDataSaveTool;
import com.huichenghe.xinlvsh01.Utils.MyConfingInfo;


/**
 * 广播接收器，接收系统发出的短信广播
 *
 * 注意：此广播要在minifes.xml中注册
 * Created by lixiaoning on 15-12-2.
 */
public class SmsReciver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle bundle = intent.getExtras();
        SmsMessage msg = null;
        if(null != bundle)
        {
            Object[] smsObj = (Object[])bundle.get("pdus");
            for (Object object : smsObj)
            {
                msg = SmsMessage.createFromPdu((byte[]) object);

                Log.i("SMS", "sms, number:" + msg.getOriginatingAddress()
                        + "   body:" + msg.getDisplayMessageBody() + "   time:"
                        + msg.getTimestampMillis());

                String smsR = LocalDataSaveTool.getInstance(context).readSp(MyConfingInfo.SMS_REMIND_CHANGE);
                boolean isNeedRemind = false;
                if(smsR.equals(MyConfingInfo.REMIND_OPEN))
                {
                    isNeedRemind = true;
                }

                if(isNeedRemind)   // 用户已打开短信提醒
                {
                    // 查询contentProvider，若有联系人姓名，则发送提醒信息
                    String number = msg.getOriginatingAddress();        // 获取电话号码
                    String contactName = BreakPhoneUtils.getContactNameFromPhoneBook(context, number);
                    if(contactName != null && !contactName.equals(""))  // 有联系人名字
                    {
                        // 打开短信提醒

                        BleForPhoneAndSmsRemind remind = new BleForPhoneAndSmsRemind();
                        remind.openThePhoneAndSmsRemind((byte)0x02);

                        // 发送提醒数据
                        remind.startPhoneAndSmsRemind(number, contactName, BleForPhoneAndSmsRemind.startSmsRemindToDevice);
                    }



                }


            }
        }






    }
}
